﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using mscook.core;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.mvc.core;
using mscook.core.services;
using System.Web.Security;
using mscook.mvc.Core;
using mscook.mvc.Areas.weblogin.Models;
using System.Linq.Expressions;

namespace mscook.mvc.Areas.weblogin.Controllers
{
    [UserAuthorizeAttribute(Roles = "admin")]
    public class LoginUserOfPresentController : BaseController
    {
        private ILoginUserService iLoginUserService;
        private ILoginUserOfPresentRepository iLoginUserOfPresentRepository;
        public LoginUserOfPresentController(ILoginUserService iLoginUserService, ILoginUserOfPresentRepository iLoginUserOfPresentRepository)
            : base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
            this.iLoginUserOfPresentRepository = iLoginUserOfPresentRepository;
        }

        public ActionResult Index(string nowPageNum, string per = "",string userName="",string present="")
        {
            Expression<Func<LoginUserOfPresent, bool>> select_expression = o => (string.IsNullOrEmpty(userName) || o.LoginUser.LoginName.Equals(userName))
                && (string.IsNullOrEmpty(present) || o.Present.Name.Contains(present));

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<LoginUserOfPresent> iLoginUserOfVipList = iLoginUserOfPresentRepository.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total, select_expression);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitNavBar();

            ViewData["userName"] = userName;
            ViewData["present"] = present;
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            return View(iLoginUserOfVipList);
        }
        [HttpGet]
        public ActionResult Edit(string id)
        {
            LoginUserOfPresent loginUserOfPresent = iLoginUserOfPresentRepository.Get(id);
            if (loginUserOfPresent != null)
            {
                LoginUserOfPresentModel loginUserOfPresentModel = new LoginUserOfPresentModel() { LoginUserOfPresent = loginUserOfPresent, LoginUser = CurrentUser };

                InitNavBar();
                return View(loginUserOfPresentModel);
            }
            return RedirectToIndex();
        }

        [HttpPost]
        public ActionResult Edit(string id, LoginUserOfPresentModel loginUserOfPresentModel)
        {
            LoginUserOfPresent loginUserOfPresent = iLoginUserOfPresentRepository.Get(id);
            if (ModelState.IsValid)
            {
                if (loginUserOfPresent != null)
                {
                    loginUserOfPresent.IsPassed = loginUserOfPresentModel.IsPassed;
                    iLoginUserOfPresentRepository.SaveOrUpdate(loginUserOfPresent);
                }
            }
            return RedirectToIndex();
        }
       
    }
}
